package com.ruoyi.framework.datasource;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;

/**
 * 
 * @date 2020/12/5 3:13 下午
 */
public class DBPassWordKeyConstant {
    /**
     * 私钥
     */
    private  static final String DEV_PRIVATE_KEY="MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKs+g9jJte3ZwFkxwjiQ8nn/+YBhO8Al7KDEq4AG96ecBRZ60js7KZJ+GYSETt76exS5id+B0rtGRF4Axrbk+55DCx9zC7fvjQSJu8URcKnCk2olmS8I2rAe+eJ1pJb0qHHY0FQHgwQYIh+5P99FNiBl6kpST8GaYnvQqyCUfcHJAgMBAAECgYAyl3uYdq97H9QGBf9deuVISenKle9zK0EnljMc1fIg4KVYKN0nhqkXkI21aulqUxI94zrqf45kqbPVGeGbczcq5MsZXdyqkK8HmHdhzGeOAaAUs85RJH42PX0/UF45OTJRpwc/bvH9AxD6IJFFID4Q/pHFX0+yQDTDsEwxoxxNoQJBANqRDaDahcx7UVpkGQg86bF7EFLeaWz8dQzMlz3bRTJjwhTKU9FeJt4zY32Vq8D8Ihg0rLboxVFwXnLJ1JtU5uUCQQDIkqIwrK57r9Qj5+sG/Uws8GPtFI/JGFgOnSOwnPPWy2e9+38jUucnto2sCA2upxVGS40lFGh3f9qA3c6dFX0VAkEA1JBHuc1sXM798rPw6GMdQB+YY4+s5nKsf5Ml7+GlbXk5krCnTbKMiINf2g5K6LgNjI8F/ilpZWesY2TI4XxzxQJAE2vvfJscAioSdq+cWH7qg2d7pZdaHPTYggHBVPa9vczQbNsPgpIig/vvV/snqAPR0Y67Sh3hx8B/nf7cZOWRyQJBALF6vzFFImv8vK2VYHLE/M6xAcp1B0ZhHdS0bSb9g1XAE2AoU8hXU1q3D0AoH3dygiOY5EERLXmpzU0GWsHInmE=";
    private  static final String PROD_PRIVATE_KEY="MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAKs+g9jJte3ZwFkxwjiQ8nn/+YBhO8Al7KDEq4AG96ecBRZ60js7KZJ+GYSETt76exS5id+B0rtGRF4Axrbk+55DCx9zC7fvjQSJu8URcKnCk2olmS8I2rAe+eJ1pJb0qHHY0FQHgwQYIh+5P99FNiBl6kpST8GaYnvQqyCUfcHJAgMBAAECgYAyl3uYdq97H9QGBf9deuVISenKle9zK0EnljMc1fIg4KVYKN0nhqkXkI21aulqUxI94zrqf45kqbPVGeGbczcq5MsZXdyqkK8HmHdhzGeOAaAUs85RJH42PX0/UF45OTJRpwc/bvH9AxD6IJFFID4Q/pHFX0+yQDTDsEwxoxxNoQJBANqRDaDahcx7UVpkGQg86bF7EFLeaWz8dQzMlz3bRTJjwhTKU9FeJt4zY32Vq8D8Ihg0rLboxVFwXnLJ1JtU5uUCQQDIkqIwrK57r9Qj5+sG/Uws8GPtFI/JGFgOnSOwnPPWy2e9+38jUucnto2sCA2upxVGS40lFGh3f9qA3c6dFX0VAkEA1JBHuc1sXM798rPw6GMdQB+YY4+s5nKsf5Ml7+GlbXk5krCnTbKMiINf2g5K6LgNjI8F/ilpZWesY2TI4XxzxQJAE2vvfJscAioSdq+cWH7qg2d7pZdaHPTYggHBVPa9vczQbNsPgpIig/vvV/snqAPR0Y67Sh3hx8B/nf7cZOWRyQJBALF6vzFFImv8vK2VYHLE/M6xAcp1B0ZhHdS0bSb9g1XAE2AoU8hXU1q3D0AoH3dygiOY5EERLXmpzU0GWsHInmE=";

    /**
     * 公钥
     */
    private static final String DEV_PUBLIC_KEY="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrPoPYybXt2cBZMcI4kPJ5//mAYTvAJeygxKuABvennAUWetI7OymSfhmEhE7e+nsUuYnfgdK7RkReAMa25PueQwsfcwu3740EibvFEXCpwpNqJZkvCNqwHvnidaSW9Khx2NBUB4MEGCIfuT/fRTYgZepKUk/BmmJ70KsglH3ByQIDAQAB";
    private static final String PROD_PUBLIC_KEY="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrPoPYybXt2cBZMcI4kPJ5//mAYTvAJeygxKuABvennAUWetI7OymSfhmEhE7e+nsUuYnfgdK7RkReAMa25PueQwsfcwu3740EibvFEXCpwpNqJZkvCNqwHvnidaSW9Khx2NBUB4MEGCIfuT/fRTYgZepKUk/BmmJ70KsglH3ByQIDAQAB";


    /**
     * 根据环境获取解密公钥
     * @param type
     * @return
     */
    public static String encryptPwdByProfile(String pwd, String type){
        if("dev".equals(type)){
            return SecureUtil.rsa(DEV_PRIVATE_KEY, null).encryptBase64(pwd, KeyType.PrivateKey);
        }
        if("prod".equals(type)){
            return SecureUtil.rsa(PROD_PRIVATE_KEY, null).encryptBase64(pwd, KeyType.PrivateKey);
        }
        return "";
    }


    public static String getPwdByProfile(String type, String enPwd){
        if("dev".equals(type)){
            return SecureUtil.rsa(null, DEV_PUBLIC_KEY).decryptStr(enPwd, KeyType.PublicKey);
        }
        if("prod".equals(type)){
            return SecureUtil.rsa(null, PROD_PUBLIC_KEY).decryptStr(enPwd, KeyType.PublicKey);
        }
        return "";
    }

    public static void main(String[] args) {

        String pwd = ")Ym*MaTDxaC59c^veGqiV6lC";
        String type = "dev";

        String enPwd = encryptPwdByProfile(pwd, type);
        String pwdByProfile = getPwdByProfile(type, enPwd);

        System.out.println(enPwd);
        System.out.println(pwdByProfile);
    }
}
